/**
 * @author: 94
 */
import { apiApp } from '@/services/api';
import { qiankunStart } from 'umi';

const Model = {
  namespace: 'Global',

  state: {
    name: 'Yay! Welcome to umi!',
    app: {},
    data: {},
  },

  effects: {
    * fetchApp ({ payload, callback }, { call, put }) {
      const res = yield call(apiApp);
      const { status, data } = res;
      if (status === 200 && data) {
        const { app = [] } = data;
        let appBaseMap = {};
        app.forEach(v => {
          appBaseMap[v.base] = v;
        });
        yield put({
          type: 'save',
          payload: {
            app: appBaseMap,
          },
        });
        qiankunStart();
      }
      if (callback) callback(res);
    },
    * modifyData({ payload }, { put }) {
      yield put({
        type: 'save',
        payload
      })
    }
  },
  reducers: {
    save (state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default Model;
